﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.DocumentManagement
{
    public class Document : DetailView<document>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<document> EntityList;
        private new document Entity;

        public long TransactionId { get; private set; }

        // ReSharper restore InconsistentNaming

        #region Overrides of DetailView<document>

        public override List<document> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.documents.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<document> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.documents.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override document GetEntity()
        {
            _context = new project5188Entities();
            Entity = new document();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTodocuments(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        public long GetLastDocumentId()
        {
            _context = new project5188Entities();
            var maxNumber = (from d in _context.documents
                              select (long?)d.a_ID).Max();

            return maxNumber.HasValue ? maxNumber.Value : 0;
        }

        public List<document> GetEntityListByParameters(string documentNoText, string documentName, string version, long documentStatusId)
        {
            _context = new project5188Entities();

            var parameterList = new List<ObjectParameter>();

            var dynamicString = "it.IsDeleted = @IsDeleted";
            parameterList.Add(new ObjectParameter("IsDeleted", false));

            if (!string.IsNullOrEmpty(documentNoText) && !string.IsNullOrWhiteSpace(documentNoText))
            {
                dynamicString += " And it.a_ID = @DocumentNo";
                parameterList.Add(new ObjectParameter("DocumentNo", Convert.ToInt64(documentNoText)));
            }

            if (!String.IsNullOrEmpty(documentName) && !String.IsNullOrWhiteSpace(documentName))
            {
                dynamicString += " And it.b_DocumentName = @DocumentName";
                parameterList.Add(new ObjectParameter("DocumentName", documentName));
            }

            if (!String.IsNullOrEmpty(version) && !String.IsNullOrWhiteSpace(version))
            {
                dynamicString += " And it.d_Version = @Version";
                parameterList.Add(new ObjectParameter("Version", version));
            }

            if (documentStatusId > 0)
            {
                dynamicString += " And it.g_DocumentStatusParameterValueID_27 = @DocumentStatusId";
                parameterList.Add(new ObjectParameter("DocumentStatusId", documentStatusId));
            }

            EntityList = _context.documents.Where(dynamicString, parameterList.ToArray()).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        #endregion
    }
}
